/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.oa.service.outgoing;

import java.util.List;
import java.util.Map;

import org.activiti.engine.IdentityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.google.common.collect.Maps;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.act.service.ActTaskService;
import com.jeeplus.modules.oa.entity.outgoing.OaOutgoing;
import com.jeeplus.modules.oa.mapper.outgoing.OaOutgoingMapper;

/**
 * 外出表Service
 * @author lpq
 * @version 2017-07-27
 */
@Service
@Transactional(readOnly = true)
public class OaOutgoingService extends CrudService<OaOutgoingMapper, OaOutgoing> {

	@Autowired
	private ActTaskService actTaskService;
	
	@Autowired
	private IdentityService identityService;
	
	public OaOutgoing getByProcInsId(String procInsId) {
		return mapper.getByProcInsId(procInsId);
	}
	
	
	/**
	 * 审核新增或编辑
	 * @param oaOutgoing
	 */
	@Transactional(readOnly = false)
	public void save(OaOutgoing oaOutgoing) {
		
		// 申请发起
		if (StringUtils.isBlank(oaOutgoing.getId())){
			oaOutgoing.preInsert();
			mapper.insert(oaOutgoing);
			
			
			// 用来设置启动流程的人员ID，引擎会自动把用户ID保存到activiti:initiator中
			identityService.setAuthenticatedUserId(oaOutgoing.getCurrentUser().getLoginName());
			// 启动流程
			actTaskService.startProcess("oaOutgoing", "oa_outgoing", oaOutgoing.getId(), oaOutgoing.getContent());
			
		}
		
		// 重新编辑申请		
		else{
			oaOutgoing.preUpdate();
			mapper.update(oaOutgoing);

			oaOutgoing.getAct().setComment(("1".equals(oaOutgoing.getAct().getFlag())?"[重申] ":"[销毁] ")+oaOutgoing.getAct().getComment());
			
			// 完成流程任务
			Map<String, Object> vars = Maps.newHashMap();
			vars.put("pass",oaOutgoing.getAct().getFlag());
			actTaskService.complete(oaOutgoing.getAct().getTaskId(), oaOutgoing.getAct().getProcInsId(), oaOutgoing.getAct().getComment(), oaOutgoing.getContent(), vars);
		}
	}
	
	
	
	
	/**
	 * 审核审批保存
	 * @param oaOutgoing
	 */
	@Transactional(readOnly = false)
	public void auditSave(OaOutgoing oaOutgoing) {
		
		// 设置意见
		oaOutgoing.getAct().setComment(("1".equals(oaOutgoing.getAct().getFlag())?"[同意] ":"[驳回] ")+oaOutgoing.getAct().getComment());
		
		oaOutgoing.preUpdate();
		
		// 对不同环节的业务逻辑进行操作
		String taskDefKey = oaOutgoing.getAct().getTaskDefKey();

		// 审核环节
		if ("audit".equals(taskDefKey)){
			
		}
		
		// 提交流程任务
		Map<String, Object> vars = Maps.newHashMap();
		vars.put("pass",oaOutgoing.getAct().getFlag());
		actTaskService.complete(oaOutgoing.getAct().getTaskId(), oaOutgoing.getAct().getProcInsId(), oaOutgoing.getAct().getComment(), vars);
		
	}

	public OaOutgoing get(String id) {
		return super.get(id);
	}
	
	public List<OaOutgoing> findList(OaOutgoing oaOutgoing) {
		return super.findList(oaOutgoing);
	}
	
	public Page<OaOutgoing> findPage(Page<OaOutgoing> page, OaOutgoing oaOutgoing) {
		return super.findPage(page, oaOutgoing);
	}
	
	
	@Transactional(readOnly = false)
	public void delete(OaOutgoing oaOutgoing) {
		super.delete(oaOutgoing);
	}
	
}